﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace XGameFrame
{
    public class MapData
    {
        /// <summary>
        /// Author of map
        /// </summary>
        public String Author { get; set; }

        /// <summary>
        /// Map heigth
        /// </summary>
        public int MapHeigth { get; set; }

        /// <summary>
        /// Map width
        /// </summary>
        public int MapWidth { get; set; }

        /// <summary>
        /// Map path
        /// </summary>
        public String MapPath { get; set; }

        /// <summary>
        /// Path to xml file
        /// </summary>
        private String XmlPath { get; set; }


        public XDocument XmlData { get; set; }

        public MapData(String xmlPath)
        {
            try
            {
                XmlData = XDocument.Load(xmlPath);
                Author = getAuthor();
                MapWidth = getMapWidth();
                MapHeigth = getMapHeigth();
                MapPath = getMapPath();

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private String getAuthor()
        {
            try
            {
                var personen = from a in XmlData.Elements()
                               select a.Element("Author").Value;
                return personen.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private int getMapWidth()
        {
            try
            {
                var width = from a in XmlData.Elements()
                               select a.Element("MapWidth").Value;
                return Convert.ToInt32(width);
            }
            catch (Exception)
            {
                throw;
            }
        }


        private int getMapHeigth()
        {
            try
            {
                var heigth = from a in XmlData.Elements()
                            select a.Element("MapWidth").Value;
                return Convert.ToInt32(heigth);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private String getMapPath()
        {
            try
            {
                var path = from a in XmlData.Elements()
                               select a.Element("MapPath").Value;
                return path.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
